package com.example.demo.utils;

import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName updownchange
 * @Description TODO
 * Author yangqinglin
 * @Date 2021/1/21 19:49
 * @Version
 **/
@Slf4j(topic = "updowm")
public class updownchange {

    static Object ob = new Object();

    static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Room room = new Room();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {

                    room.Increament();


            }
        }, "t1-add");


        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {

                    room.decreament();


            }
        }, "t2-delete");
        t1.start();
        t2.start();


        t1.join();
        t2.join();
        System.out.println(room.getCount());

    }


}

class Room {

    private int count = 0;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public synchronized void Increament() {

            count++;


    }

    public void decreament() {
        synchronized (this) {
            count--;
        }

    }


}
